/*  dreic -- a compiler for Drei
 *
 *  $Id: GeneratorTest.scala 3228 2006-12-07 02:05:31Z dubochet $
 */

package dreic

/**
 * usage: scala dreic.GeneratorTest <file.drei> [ <file.asm> ]
 */
object GeneratorTest {
  import java.io.{FileInputStream, PrintWriter, FileWriter, IOException}

  def main(args: Array[String]) = {
    if (args.length == 0) {
      Report.fail("usage: scala dreic.GeneratorTest <file.drei> [ <file.asm> ]")
    }
    try {
      val in = new FileInputStream(args(0))
      val tree = new Parser(in).parse
      in.close()
      if (Report.errors > 0)
        System.err.println(Report.errors + " errors found.")
      else {
        val analyzer = new Analyzer()
        analyzer.analyzeProgram(tree)
        if (Report.errors == 0) {
          val out = if (args.length < 2) new PrintWriter(System.out)
                    else new PrintWriter(new FileWriter(args(1)))
          val generator = new Generator(analyzer)
          generator.gen(tree)
          generator.code.write(out)
        }
      }
      Report.exit();
    }
    catch {
      case e: IOException => Report.fail(e.getMessage())
    }
  }

}
